/*
 * DiscoveryModel.java
 * business
 *
 * Created by ChenTao on 2016/10/19.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.WantToBuyEntity;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.TopArticle;
import com.yidiandao.business.data.request.DiscoveryItemPageRequest;
import com.yidiandao.business.data.request.DiscoveryTypeRequest;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.response.DiscoveryItemPageResponse;
import com.yidiandao.business.data.response.DiscoveryItemTypeResponse;
import com.yidiandao.business.data.response.DiscoveryMainTypeResponse;
import com.yidiandao.business.data.response.DiscoveryWantToBuyPageResponse;
import com.yidiandao.business.data.response.QueryDiscountResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现页相关Model
 */
public class DiscoveryModel implements IDiscoveryModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<QueryDiscountResponse.Discount> mDiscountDisableItems = new ArrayList<>();

    private List<Object> mDiscoveryData = new ArrayList<>();

    private int discountPager;

    private int page;

    @Override
    public void loadDiscountList(
            final boolean isRefreshing,
            String tag,
            final OnLoadDiscoveryListener<List<QueryDiscountResponse.Discount>> listener) {

        if (isRefreshing) {
            discountPager = 1;
        }
        PagerRequest request = new PagerRequest();
        request.setTag(tag);
        request.setUrl(Urls.DISCOUNT);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(discountPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(
                request,
                QueryDiscountResponse.class,
                new NetListener<QueryDiscountResponse>() {
                    @Override
                    public void onSuccess(QueryDiscountResponse response) {

                        // pager 增加
                        discountPager++;

                        if (isRefreshing) {
                            mDiscountDisableItems.clear();
                        }

                        QueryDiscountResponse.Data data = response.getData();

                        List<QueryDiscountResponse.Discount> discountList = data.getDiscount();
                        if (discountList != null && discountList.size() != 0) {
                            mDiscountDisableItems.addAll(discountList);
                        }

                        if (listener != null) {
                            listener.onSuccess(
                                    mDiscountDisableItems,
                                    discountList == null || discountList.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }


    @Override
    public void loadMainTitle(String tag, final OnAccessNetListener<List<DiscoveryMainTypeResponse.CatTypeEntity>> listener) {

        BaseRequest request = new BaseRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DISCOVERY_CATTYPE);
        mNetBiz.sendGetRequest(request, DiscoveryMainTypeResponse.class, new NetListener<DiscoveryMainTypeResponse>() {
            @Override
            public void onSuccess(DiscoveryMainTypeResponse discoveryTypeResponse) {
                DiscoveryMainTypeResponse.Data data = discoveryTypeResponse.getData();
                List<DiscoveryMainTypeResponse.CatTypeEntity> catType = data.getCatType();
                if (null != listener)
                    listener.onSuccess(catType);
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void loadItemTitle(String tag, String typeId, final OnAccessNetListener<List<DiscoveryItemTypeResponse.DataBean.CatListBean>> listener) {

        DiscoveryTypeRequest request = new DiscoveryTypeRequest();
        request.setTypeId(typeId);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DISCOVERY_CAT_LIST);
        mNetBiz.sendGetRequest(request, DiscoveryItemTypeResponse.class, new NetListener<DiscoveryItemTypeResponse>() {
            @Override
            public void onSuccess(DiscoveryItemTypeResponse discoveryTypeResponse) {
                DiscoveryItemTypeResponse.DataBean data = discoveryTypeResponse.getData();
                List<DiscoveryItemTypeResponse.DataBean.CatListBean> catList = data.getCatList();
                if (null != listener)
                    listener.onSuccess(catList);
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void loadDiscoveryPageData(String tag, String catId, final boolean isRefresh, final OnLoadDiscoveryListener<List<Object>> listener) {
        if (isRefresh) {
            page = 1;
        }
        DiscoveryItemPageRequest request = new DiscoveryItemPageRequest();
        request.setCatId(catId);
        request.setPage(page);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setUrl(Urls.DISCOVERY_INDEX);
        mNetBiz.sendGetRequest(request, DiscoveryItemPageResponse.class, new NetListener<DiscoveryItemPageResponse>() {
            @Override
            public void onSuccess(DiscoveryItemPageResponse response) {
                page++;
                if (isRefresh) {
                    mDiscoveryData.clear();
                }
                DiscoveryItemPageResponse.Data data = response.getData();
                if (null != data) {
                    List<Article> articleList = data.getArticleList();
                    for (int i = 0; i < articleList.size(); i++) {
                        Article bean = articleList.get(i);
                        String thumbType = bean.getThumbType();
                        if (thumbType.equals("1")) {
                            mDiscoveryData.add(new TopArticle(bean));
                        } else {
                            mDiscoveryData.add(bean);
                        }
                    }
                }

                if (listener != null) {
                    listener.onSuccess(mDiscoveryData, mDiscoveryData.size() == 0, response.hasMoreData());
                }

            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener)
                    listener.onFailed();
                return true;
            }


            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void loadWantToBuyPageData(final boolean isRefreshing, String tag, final OnLoadDiscoveryListener<List<Object>> listener) {

        if (isRefreshing) {
            page = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setUrl(Urls.DISCOVERY_WANT_TO_BUY);
        request.setPage(page);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, DiscoveryWantToBuyPageResponse.class, new NetListener<DiscoveryWantToBuyPageResponse>() {
            @Override
            public void onSuccess(DiscoveryWantToBuyPageResponse response) {

                page++;

                if (isRefreshing) {
                    mDiscoveryData.clear();
                }

                List<WantToBuyEntity> wantBuyList = response.getData().getWantBuyList();
                mDiscoveryData.addAll(wantBuyList);

                if (listener != null) {
                    listener.onSuccess(mDiscoveryData, mDiscoveryData.size() == 0, response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) listener.onFailed();
                return false;
            }

            @Override
            public void onFinal() {

            }
        });

    }

}
